﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Jaiden.Net.Http
{
    class JaidenRequest
    {
        public JaidenRequest()
        {

        }
        public async Task Load(HttpRequestMessage message)
        {
            this.Method = message.Method.Method;
            this.RequestUri = message.RequestUri;
            this.LoadHeaders(message.Headers);
            if (message.Content != null)
            {
                this.LoadHeaders(message.Content.Headers);
                this.Data = await message.Content.ReadAsByteArrayAsync();
            }

        }

        public byte[] Data { get; set; }

        public Dictionary<string,List<string>> Headers { get; } = new Dictionary<string, List<string>>();

        private void LoadHeaders(HttpHeaders headers)
        {
            if (headers != null && headers.Any())
            {
                foreach (var header in headers)
                {
                    var key = header.Key;
                    var values = header.Value.ToList();
                    if (!this.Headers.ContainsKey(key))
                    {
                        this.Headers[key] = new List<string>();
                    }
                    this.Headers[key].AddRange(values);
                }
            }
        }

        public String Method { get; set; }

        public Uri RequestUri { get; set; }
    }
}
